default_status = 'To post on this fan page you must first become a fan';

function setMenuBehavior(url)
{
	jQuery('.stream_header_menu_item_on').mouseover(function(){}).mouseout(function(){}).click(function(){});
	jQuery('.stream_header_menu_item_off').mouseover(function(){
		jQuery(this).find("span").css("text-decoration","underline");
		jQuery(this).find("span").css("cursor","pointer");
	}).mouseout(function(){
		jQuery(this).find("span").css("text-decoration","none");
		jQuery(this).find("span").css("cursor","default");
	}).click(function(){
		var id = jQuery(this).attr("id");
		if (id!="filter"+selectedFilter)
		{
			jQuery(".loader_div").show(500);
			if (id=='filter0')
			{
				jQuery("#filter0").removeClass().addClass('stream_header_menu_item_on').mouseover(function(){}).mouseout(function(){}).click(function(){});
				jQuery("#filter"+selectedFilter).removeClass().addClass('stream_header_menu_item_off');
				selectedFilter =0;
                                var post_data = "page=stSocialMediaToolbox&type=getAll&action=getFbFeed&pid="+pid+"&ajax=1&start=0&url="+url;
                                var ajax_url = url + '?' +post_data;
				// Get Stream for all
				jQuery.ajax({
					type: "POST",
					url: ajax_url,
					data: post_data,
					success: function(msg){
						jQuery(".socialmediatoolboxes_tw_stream_body").html(msg);
						jQuery(".loader_div").hide(500);
					}
				});
			}
			else if (id=='filter1')
			{
				jQuery("#filter1").removeClass().addClass('stream_header_menu_item_on').mouseover(function(){}).mouseout(function(){}).click(function(){});
				jQuery("#filter"+selectedFilter).removeClass().addClass('stream_header_menu_item_off');
				selectedFilter =1;
                                var post_data = "page=stSocialMediaToolbox&type=getOnlyPage&action=getFbFeed&pid="+pid+"&ajax=1&start=0&url="+url;
                                var ajax_url = url + '?' +post_data;
				// Get Stream for Page
				jQuery.ajax({
					type: "POST",
					url: ajax_url,
					data: post_data,
					success: function(msg){
						jQuery(".socialmediatoolboxes_tw_stream_body").html(msg);
						jQuery(".loader_div").hide(500);
					}
				});

				_fun_registeraction(6,"publisher","filter");
			}
			else if (id=='filter2')
			{
				jQuery("#filter2").removeClass().addClass('stream_header_menu_item_on').mouseover(function(){}).mouseout(function(){}).click(function(){});
				jQuery("#filter"+selectedFilter).removeClass().addClass('stream_header_menu_item_off');
				selectedFilter =2;
                                var post_data = "page=stSocialMediaToolbox&type=getOnlyFans&action=getFbFeed&pid="+pid+"&ajax=1&start=0&url="+url;
                                var ajax_url = url + '?' +post_data;
				// Get Stream for all
				jQuery.ajax({
					type: "POST",
					url: ajax_url,
					data: post_data,
					success: function(msg){
						jQuery(".socialmediatoolboxes_tw_stream_body").html(msg);
						jQuery(".loader_div").hide(500);
					}
				});

				_fun_registeraction(7,"fans","filter");
			}
			setMenuBehavior(url);
		}
	});
}

function load_comments(post_id)
{
	jQuery("#comment_box_"+post_id).find(".comments_loader").css("display","inline");
	jQuery.ajax({
		type: "POST",
		url: "ajax.php",
		data: "action=getComments&post_id="+post_id+"&pid="+pid,
		success: function(msg){
			jQuery("#comment_box_"+post_id).html(msg);
		}
	});
}

function getMorePosts(action,start, url)
{
	jQuery("#imgLoadingMore").fadeIn();
	switch (action)
	{
		case 'getAll':
                    var post_data = "page=stSocialMediaToolbox&type=getAll&action=getFbFeed&pid="+pid+"&ajax=1&start="+start+"&url="+url;
                    var ajax_url = url+'?'+ post_data;
			jQuery.ajax({
				type: "POST",
				url:ajax_url,
				data: post_data,
				success: function(msg){
					jQuery("#imgLoadingMore").fadeOut();
					jQuery(".socialmediatoolboxes_tw_stream_body").html(msg);
				}
			});
			break;
		case 'getOnlyPage':
                    var post_data = "page=stSocialMediaToolbox&type=getOnlyPage&action=getFbFeed&pid="+pid+"&ajax=1&start="+start+"&url="+url;
                    var ajax_url = url+'?'+ post_data;
			jQuery.ajax({
				type: "POST",
				url: ajax_url,
				data: post_data,
				success: function(msg){
					jQuery("#imgLoadingMore").fadeOut();
					jQuery(".socialmediatoolboxes_tw_stream_body").html(msg);
				}
			});
			break;
		case 'getOnlyFans':
                     var post_data = "page=stSocialMediaToolbox&type=getOnlyFans&action=getFbFeed&pid="+pid+"&ajax=1&start="+start+"&url="+url;
                     var ajax_url = url+'?'+ post_data;
			jQuery.ajax({
				type: "POST",
				url: ajax_url,
				data: post_data,
				success: function(msg){
					jQuery("#imgLoadingMore").fadeOut();
					jQuery(".socialmediatoolboxes_tw_stream_body").html(msg);
				}
			});
			break;
	}

}

function onConnected() {
	// Wait until Connection established
	FB.Facebook.get_sessionState().waitUntilReady(function(){
		var uid = FB.Facebook.apiClient.get_session().uid;
		FB.Facebook.apiClient.pages_isFan(pid, uid, function(res) {
			if (res) {
				default_status = 'Post on this fan page';
				jQuery("#user_panel").html(getTop(uid,1));
				jQuery("#shoutbox_input").focus(function () {
					if (jQuery(this).val() == default_status) {
						jQuery(this).val('');
					}
				});
				jQuery("#shoutbox_input").blur(function () {
					if (jQuery(this).val() == '') {
						jQuery(this).val(default_status);
					}
				});
				FB.XFBML.Host.parseDomTree();
			} else {
				onNotFan(uid);
			}
		});
	});
}

function onNotConnected() {
	jQuery("#user_panel").html(getTop(0,0));
	FB.XFBML.Host.parseDomTree();
}

function onNotFan(uid) {
	jQuery("#user_panel").html(getTop(uid,0));
	FB.XFBML.Host.parseDomTree();
}

function UpdateStatus() {
	FB.Facebook.apiClient.users_hasAppPermission('publishStream', function(result) {
		// prompt offline permission
		if (result == 0) {
			// render the permission dialog
			FB.Connect.showPermissionDialog('publishStream', stream_publish);
		} else {
			// permission already granted.
			var m = jQuery("#shoutbox_input").val();
			FB.Facebook.get_sessionState().waitUntilReady(function(){
				FB.Connect.streamPublish(m, null, null, pid, default_status, function() {
                                    // Delete memcache
//                                    jQuery.ajax({
//                                            type: "POST",
//                                            url: "clearmem.php",
//                                            data: "fpid="+pid,
//                                            success: function(){
//                                                    window.location = window.location.href;
//                                            }
//                                    });
				});
			});
		}
	});
}

function getTop(uid,fan) {
	var out ='';
	
	var text = '';
	if (uid == 0 || fan == 0) {
		text = ' disabled="disabled" ';
	}
        if (uid == 0)
        {
            default_status = "In order to post a message on the fan page you need to connect first";
        }
        else
        {
            default_status = 'To post on this fan page you must first become a fan';
        }

        if (uid != 0 && fan != 0) {
		default_status = 'Post on this fan page';
	}
	
	out +='<table width="100%">';
	out += '<tr height="77">';
	out +='	<td align="center">';
	out +='		<table width="100%" height="75">';
	out +='			<tr>';
	out +='				<td align="center" valign="top" width="60">';
	if (uid == 0) {
		out +='					<img class="FB_profile_pic fb_profile_pic_rendered" title="Not Connected" alt="Not Connected" src="../css/frontend/facebook/images/fbpic.png">';
	} else {
		out +='					<fb:profile-pic uid="'+uid+'" size="square" linked="false" facebook-logo="true">';
		out +='					</fb:profile-pic>';
		out +='					<a href="javascript:void(0);" onclick="logOut();" class="link_normal">Logout</a>';
	}
	out +='				</td>';
	out +='				<td align="left" valign="top">';
	out +='					<div class="status_textbox">';
	out +='					<textarea rows="2" title="'+default_status+'" class="shoutbox_textarea" id="shoutbox_input" name="shoutbox_textarea" '+text+'>'+default_status+'</textarea>';
	if (uid != 0 && fan == 0) {
		out +='					<span class="link_normal" style="float:left;margin-left:12px;">Already a fan? <a href="" class="link_normal" style="text-decoration:underline;">click here to refresh</a></span>';
	}
        if (uid!=0){
                out +='					<a onclick="this.blur(); UpdateStatus(); _fun_registeraction(5,\'\',\'\');" class="button_wide1" href="javascript:void(0);"><img border="0" src="images/post.gif"></a>';
        }
        else{
                        out +='                 <fb:login-button size="fb:login-butto" background="white" class=" fb_login_not_authorized FB_login_button FB_ElementReady" >';
        out +='                 </fb:login-button>';
        }
	out +='				</td>';
	out +='			</tr>';
	out +='		</table>';
	out +='	</td>';
	out +='</tr>';
	out +='</table>';

	return out;
}

function logOut() {
	FB.Connect.logoutAndRedirect(window.location.href);
}
